﻿#include <iostream>
#include <opencv2\opencv.hpp>

//参考链接https://blog.csdn.net/weixin_40647819/article/details/90179953 

void my_test(std::string filename)
{

	cv::Mat src = cv::imread(filename);
	if (src.empty()) {
		return;
	}

	if (src.channels() > 1)
	{
		cv::cvtColor(src, src, CV_RGB2GRAY);//都弄成灰的
	}

	cv::Mat dst;
	int Otsu = 0;
	double t2 = (double)cv::getTickCount();
	Otsu = cv::threshold(src, dst, Otsu, 255, CV_THRESH_OTSU + CV_THRESH_BINARY);
	std::cout << "Otsu=" << Otsu << std::endl;
	t2 = (double)cv::getTickCount() - t2;
	double cost_time = (t2 * 1000.) / ((double)cv::getTickFrequency());
	std::cout << "cost_time=" << cost_time << " ms. " << std::endl << std::endl;


	cv::namedWindow(filename + "_src", CV_WINDOW_NORMAL);
	cv::imshow(filename + "_src", src);

	cv::namedWindow(filename + "_OTSU", CV_WINDOW_NORMAL);
	cv::imshow(filename + "_OTSU", dst);
}

//3. 在OpenCV安装目录下找到课程对应演示图片(安装目录\sources\samples\data)，首先计算灰度直方图(在test_calcHist做，没做在这)，进一步使用大津算法进行分割，并比较分析分割结果。
int main() {

	my_test("pic2.png");
	my_test("pic6.png");

	cv::waitKey(0);
}